package com.ocgs.web.action.storage;

import com.ocgs.base.core.constant.BaseConstants;
import com.ocgs.base.core.exception.BaseServiceException;
import com.ocgs.base.core.web.action.BaseAction;
import com.ocgs.code.service.impl.CodeServiceImpl;
import com.ocgs.model.Product;
import com.ocgs.model.StorageRecord;
import com.ocgs.service.impl.ProductServiceImpl;
import com.ocgs.service.impl.StorageServiceImpl;

import java.util.List;


/**
 * @author bryant
 * @version 1.0
 * @since 2012-05-14
 *
 */
public class StorageManageAction extends BaseAction{

    private static final long serialVersionUID = -2484651244734670052L;
    private StorageServiceImpl storageService;
    private ProductServiceImpl productService;
    private CodeServiceImpl codeService;
    private List storageList;
    private List<Product> productList;
    private StorageRecord storage;
    private String[] checkBox;
    private Product product;
    public StorageServiceImpl getStorageService() {
        return storageService;
    }

    public void setStorageService(StorageServiceImpl storageService) {
        this.storageService = storageService;
    }

    public CodeServiceImpl getCodeService() {
        return codeService;
    }

    public void setCodeService(CodeServiceImpl codeService) {
        this.codeService = codeService;
    }

    public List getStorageList() {
        return storageList;
    }

    public void setStorageList(List storageList) {
        this.storageList = storageList;
    }

    public StorageRecord getStorage() {
        return storage;
    }

    public void setStorage(StorageRecord storage) {
        this.storage = storage;
    }

    public String[] getCheckBox() {
        return checkBox;
    }

    public void setCheckBox(String[] checkBox) {
        this.checkBox = checkBox;
    }

    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }

    public ProductServiceImpl getProductService() {
        return productService;
    }

    public void setProductService(ProductServiceImpl productService) {
        this.productService = productService;
    }

    public List<Product> getProductList() {
        return productList;
    }

    public void setProductList(List<Product> productList) {
        this.productList = productList;
    }

    public String loadStoragePutInPage() throws BaseServiceException {
        productList = productService.getAllProduct();
        return BaseConstants.ACTION_RESULT_PUTIN;
    }

    public String loadStorageRemovalPage() throws BaseServiceException {
        productList = productService.getAllProduct();
        return BaseConstants.ACTION_RESULT_REMOVAL;
    }

    public String loadStorageSearchPage() throws BaseServiceException {
        productList = productService.getAllProduct();
        return BaseConstants.ACTION_RESULT_SEARCH;
    }

    public String loadStorageInOutSearchPage() throws BaseServiceException {
        productList = productService.getAllProduct();
        return BaseConstants.ACTION_RESULT_IN_OUT_SEARCH;
    }

    public String PutIn() throws Exception{
        try {
            storageService.storagePutIn(storage);
            storage = new StorageRecord();
            addActionMessage(this.getText(BaseConstants.MESSAGE_PUTIN_SUCCESSFUL));
            return BaseConstants.ACTION_RESULT_PUTIN;
        } catch (BaseServiceException e) {
            e.printStackTrace();
            addActionError(e.getMessage());
            return BaseConstants.ACTION_RESULT_PUTIN;
        } finally {
            productList = productService.getAllProduct();
        }
    }

    public String removal() throws Exception {
        try {
            storageService.storageRemoval(storage);
            addActionMessage(this.getText(BaseConstants.MESSAGE_REMOVAL_SUCCESSFUL));
        } catch (BaseServiceException e) {
            e.printStackTrace();
            addActionError(this.getText(e.getMessage()));
        }finally {
            productList = productService.getAllProduct();
            return BaseConstants.ACTION_RESULT_REMOVAL;
        }

    }



    public String search() throws BaseServiceException {
        try{
            paginatedList = productService.findAllStorageForPagination(init(getRequest(), paginatedListImpl), product);
            if(paginatedList==null){
                addActionMessage(this.getText(BaseConstants.MESSAGE_NO_RECORD_FOUND));
            }
        }catch (BaseServiceException e){
            e.printStackTrace();
            addActionError(this.getText(e.getMessage()));
        }finally {
            productList = productService.getAllProduct();
            return BaseConstants.ACTION_RESULT_SEARCH;
        }
    }

    public String inAndOutSearch() throws BaseServiceException {
        try{
            paginatedList = storageService.findAllInOutStorageForPagination(init(getRequest(), paginatedListImpl), product);
            if(paginatedList==null){
                addActionMessage(this.getText(BaseConstants.MESSAGE_NO_RECORD_FOUND));
            }
        }catch (BaseServiceException e){
            e.printStackTrace();
            addActionError(this.getText(e.getMessage()));
        }finally {
            productList = productService.getAllProduct();
            return BaseConstants.ACTION_RESULT_IN_OUT_SEARCH;
        }
    }
}
